import axios from 'axios';

import Config from '../../config';
import getAPIHeaders from '../get-api-headers';
import Member from '../member/member';
import PayrollGroupMember from './payroll-group-member';

export interface GetPayrollGroupMembersWithPayoutResponse {
    id: number;
    member: Member;
    slp: number;
}

export default class PayrollGroupMemberManager {

    static getAll = async (
        payrollGroupID: number,
        page: number
    ): Promise<{
        hasMore: boolean;
        members: Array<PayrollGroupMember>;
    }> => {
        try {
            const url =
                Config.getAPI() +
                '/payroll-group-member/all?payroll_group_id=' +
                payrollGroupID +
                '&page=' +
                page;
            const call = await axios.get(url, {
                headers: await getAPIHeaders(),
            });

            const result = {
                members: [],
                hasMore: false,
            };

            result.hasMore = !call.data.no_more_results;

            if (call.data['members']) {
                const members: PayrollGroupMember[] = [];
                for (let i = 0; i < call.data['members'].length; i++) {
                    const member = new PayrollGroupMember();
                    member.id = call.data['members'][i]['id'];
                    member.member = Member.fromJSON(call.data['members'][i]['member']);
                    members.push(member);
                }

                result.members = members;
                return result;
            }
        } catch (exception) {
            console.log(exception);
        }

        return null;
    };

    static getAllWithPayouts = async (
        payrollGroupID: number
    ): Promise<Array<GetPayrollGroupMembersWithPayoutResponse>> => {
        try {
            let hasMore = true;
            let page = 0;
            const members = new Array<GetPayrollGroupMembersWithPayoutResponse>();
            
            while(hasMore) {
                const url = Config.getAPI() + '/payroll-group-member/all-with-payout' + 
                    '?payroll_group_id=' + payrollGroupID + 
                    '&page=' + page;

                const call = await axios.get(url, {
                    headers: await getAPIHeaders(),
                });

                if (call.data['members'] && call.data['members'].length > 0) {
                    for (let i = 0; i < call.data['members'].length; i++) {
                        members.push({
                            id : call.data['members'][i]['id'],
                            member : Member.fromJSON(call.data['members'][i]['member']),
                            slp : call.data['members'][i]['slp']
                        });
                    }
                    
                    page++;
                } else {
                    hasMore = false;
                }
            }

            return members;
        } catch (exception) {
            console.log(exception);
        }

        return null;
    };

    static delete = async (id: number): Promise<boolean> => {
        try {
            const url =
                Config.getAPI() + '/payroll-group-member/delete?payroll_group_member_id=' + id;
            const call = await axios.delete(url, {
                headers: await getAPIHeaders(),
            });

            return call.data['deleted'];
        } catch (exception) {
            console.log(exception);
        }

        return null;
    };

    static create = async (payroll_group_id: number, member_id: number): Promise<any> => {
        try {
            const body = { payroll_group_id, member_id };
            const url = Config.getAPI() + '/payroll-group-member/create';
            const call = await axios.post(url, body, {
                headers: await getAPIHeaders(),
            });

            return call.data;
        } catch (exception) {
            console.log(exception);
        }

        return null;
    };
}
